package com.auction.job.web.controller;

import com.auction.job.core.service.JobManagerService;
import com.auction.job.domain.request.DupBuyJobRequest;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.JsonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by nichole on 2017/8/17.
 */
@Controller
@RequestMapping(value = "/job")
public class JobManagerController extends BaseController {

    @Resource
    private JobManagerService jobManagerService;

    @RequestMapping(value = "/addJob", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult addJob(String prizeId) {
        if(StringUtils.isEmpty(prizeId)) {
            return new JsonResult(ResponseCode.PARAMETER_ERROR.code, ResponseCode.PARAMETER_ERROR.msg);
        }

        if(jobManagerService.addJob(prizeId)) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }

    @RequestMapping(value = "/addOpenJob", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult addOpenJob(String prizeId, String lastPurchaseTime) {
        if(StringUtils.isEmpty(prizeId) || StringUtils.isEmpty(lastPurchaseTime)) {
            return new JsonResult(ResponseCode.PARAMETER_ERROR.code, ResponseCode.PARAMETER_ERROR.msg);
        }

        if(jobManagerService.addOpenJob(prizeId, lastPurchaseTime)) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }

    @RequestMapping(value = "/removeJob", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult removeJob(String prizeId) {
        if(StringUtils.isEmpty(prizeId)) {
            return new JsonResult(ResponseCode.PARAMETER_ERROR.code, ResponseCode.PARAMETER_ERROR.msg);
        }

        if(jobManagerService.removeJob(prizeId)) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }

    @RequestMapping(value = "/addDupBuyJob", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult addDupBuyJob(DupBuyJobRequest jobRequest) {
        if(StringUtils.isEmpty(jobRequest.getPrizeId()) || StringUtils.isEmpty(jobRequest.getUserId())) {
            return new JsonResult(ResponseCode.PARAMETER_ERROR.code, ResponseCode.PARAMETER_ERROR.msg);
        }

        if(jobManagerService.addDupBuyJob(jobRequest)) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }

    @RequestMapping(value = "/addResumeErrorJob", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult addResumeErrorJob() {
        if(jobManagerService.addResumeErrorJob()) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }

    @RequestMapping(value = "/removeOpenJob", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult removeOpenJob(String prizeId) {
        if(jobManagerService.removeOpenJob(prizeId)) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }


    @RequestMapping(value = "/addArchiveJob", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult addArchiveJob() {
        if(jobManagerService.addArchiveJob()) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }

    @RequestMapping(value = "/removeArchiveJob", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult removeArchiveJob() {
        if(jobManagerService.removeArchiveJob()) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }

    @RequestMapping(value = "/addPushJob", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult addPushJob() {
        if(jobManagerService.addPushJob()) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }

    @RequestMapping(value = "/addReportDataJob", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult addReportDataJob() {
        if(jobManagerService.addReportDataJob()) {
            return new JsonResult();
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }
}
